<?php

namespace App\Exceptions;

class ApiException extends \Exception
{
    const ERROR_CODE = -1;
    const ERROR_MSG  = 'ApiException';

    private $data = [];

    public function __construct($res)
    {
        $this->code = $res['code'] ? : self::ERROR_CODE;
        $this->message  = $res['msg'] ? : self::ERROR_MSG;
        $this->data = $res['data'];
    }

    public function getData()
    {
        return $this->data;
    }

    public function render($request)
    {
        return response()->json([
            'code' => $this->getCode(),
            'data' => $this->getData(),
            'msg' => $this->getMessage(),
        ], 200);
    }
}
